連結:https://leetcode.com/problems/binary-tree-right-side-view/description/
class Solution {
private int maxlevel = 0;
public void right(TreeNode root,int level,List<Integer> save)
{
if (root == null)
return;
if (maxlevel < level)
{
save.add(root.val);
maxlevel = level;
}
right(root.right,level+1,save);
right(root.left,level+1,save);
}
public List<Integer> rightSideView(TreeNode root) {
List<Integer> save = new ArrayList<Integer>();
right(root,1,save);
return save;
}
}
連結:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
class Solution {
public Node connect(Node root) {
Node head = root;
for(; root != null; root = root.left)
for(Node cur = root; cur != null; cur = cur.next)
if(cur.left != null) {
cur.left.next = cur.right;
if(cur.next != null) cur.right.next = cur.next.left;
} else break;
return head;
}
}